import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import TableControls from '#cpt/temp/TableControls'
import Confirm from '#cpt/temp/Confirm'
import AddEditModal from '#cpt/temp/AddEditModal'
// ===================================================================== global declare
const { $fn, $http, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
const PreviewImage 			= $Lazy.state(()=>import('#private/pages/preview-image'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		// 搜索表单列表
		forms : [
			{ label: '标本条码',		name: 'spec_code', render:text => $fn.getSplitSpecCode(text) },
			{ label: '状态',		name: 'status',	type: 'select', loading:true },
			{ label: '时间',  		names: ['start_date','end_date'], type:'date-range',format:1 },
		],
		// 提交表单列表
		formData : [
			{ label: '标本条码',		name: 'spec_code', 		half:true, 	required:true, render: text => $fn.getSplitSpecCode(text),
				onValue:({value, formData})=> {
					if(value.length === 12 || value.length === 14){
						$http.pull(null, 'specimen/views', { param: {spec_code:value} }).then(items => {
							if($fn.hasArray(items)){
								const {patient_name, sex} = items[0]
								this.formRef.setValue({patient_name,sex:sex})
							}
						})
					}
				}
			},
			{ label: '加减类型',		name: 'item_type', 		half:true, 	type: 'select', required:true, loading:true },
			{ label: '姓名',		name: 'patient_name', 	half:true, 	required:true },
			{ label: '性别',		name: 'sex', 			half:true, 	required:true, type: 'select', loading:true, idStr:'code' },
			{ label: '项目',		name: 'kind_id', 		half:true, 	type: 'select', required:true, loading:true, 
				onValue: ({value})=> {
					$http.submit(null,'sp-customer-log/kindId',{ param:{ kind_id:value}}).then(data=>{
						this.formRef.setValue({project_name:data[0].major_group_id})
					})
				}
			},
			{ label: '时间',		name: 'op_time', 		half:true, 	type:'date-time' },
			{ label: '岗位',		name: 'project_name', 	half:true, 	type: 'select', loading:true  },
			{ label: '图片',		name: 'img', 			full:true, 	type:'upload', multiple:true, code:130 },
			{ label: '内容说明',		name: 'remark', 		full:true, 	type:'textarea' }
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.getSelect()
		
		if(window.isInit){
		  this.fetch()
		}
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		$cache.then( async f => {
			// 状态
			const statusData = $fn.setRow(forms, 'status',  await f.treatment_state)
			//
			this.setState({ forms, statusData })
		})
	}
	// 弹窗获取下拉数据
	getModalSelect = async () => {
		const { formData } = this.state
		$cache.then( async f => {
			// 加减类型
			await $fn.setRow(formData, 'item_type', await f.add_subtract)
			// 项目
			await $fn.setRow(formData, 'kind_id', await f.getCache({ api:'project_name' }) )
			// 性别
			await $fn.setRow( formData, 'sex', await f.getDicCache({ code:45700 }) )
			// 岗位
			await $fn.setRow(formData, 'project_name', await f.getCache({ api:'gang_wei' }) )
			//
			this.setState({ formData })
		})
	}
	// paging
	fetch = () => $fn.fetch.call(this,'sp-additional-item/index')
	
	// 表单
	cols = [
		{ title: '条码号',			dataIndex: 'spec_code', 		...$fn.table.spec_code },
		{ title: '姓名', 			dataIndex: 'patient_name', 		...$fn.table.name },
		{ title: '性别', 			dataIndex: 'sex_name', 			...$fn.table.sex },
		{ title: '项目名称', 		dataIndex: 'kind_name', 		width:200 },
		{ title: '时间',			dataIndex: 'op_time', 			...$fn.table.time },
		{ title: '岗位', 			dataIndex: 'project_name', 		width:200 },
		{ title: '备注说明', 		dataIndex: 'remark', 			width:250 },
		{ title: '类型', 			dataIndex: 'item_type', 		width:80, align:'center', render: text => (+text === 1 ? '加做' : '减做') },
		{ title: '状态',			dataIndex: 'status', 			width:100, render: text => $fn.getState(this.state.statusData, text) },
		TableControls.controls.call(this,{ width: 200, align:'left' })
	]
	
	// 操作按钮
	controls = () => {
		return [
			Controls.add.call(this, {click: this.getModalSelect } ),
		]
	}
	
	// 表格操作按钮
	table_controls = (text,{uuid, status, imgs}) => {
		return [
			// 编辑
			+status === 0 && TableControls.edit.call(this, 'sp-additional-item/info', {uuid},{
				select: this.getModalSelect
			} ),
			// 审核
			+status === 0 && Confirm.call(this, '审核', 'sp-additional-item/audit', { param: uuid } ),
			+status === 1 && Confirm.call(this, '完成', 'sp-additional-item/complete', { param: uuid }),
			// 查看图片
			$fn.hasArray(imgs) && TableControls.click.call(this, '查看图片', ()=>{this.previewImage?.open?.(imgs)} ),
		]
	}
	
	render(){
		return (
			<>
				<Page title='加减项' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this, { isInit: window.isInit }) }
				</Page>
				{/* 添加 */}
				{AddEditModal.call(this,{ labelWidth:'s5', width:600, api:'sp-additional-item/add',editApi:'sp-additional-item/edit' })}
				{/* 查看图片 */}
				<PreviewImage onRef	= { ref => this.previewImage = ref }/>
				
			</>
		)
	}
}

export default Index